//
//  CBCBottomMenuView.m
//  CBCReader
//
//  Created by Labanotation on 16/6/1.
//  Copyright © 2016年 okwei. All rights reserved.
//

#import "CBCBottomMenuView.h"
#import "CBCMenuView.h"
#import "CBC+UIButton.h"
#import "CBCSliderView.h"
#import "CBCFontSizeView.h"
#import "CBCColorView.h"
#import "CBCReaderDef.h"
#import "CBCReadUtilites.h"
#import "CBCReadConfig.h"
#import "CommonFuctions.h"
#if __has_include(<Masonry/Masonry.h>)
#import <Masonry/Masonry.h>
#else
#import "Masonry.h"
#endif
@interface CBCBottomMenuView ()<FontSizeChangeDelegate,TTRangeSliderDelegate>

@property (nonatomic,strong) UIButton *catalogButton;
@property (nonatomic,strong) UIButton *prgressButton;
@property (nonatomic,strong) UIButton *fontButton;
@property (nonatomic,strong) UIButton *backgroundButton;
@property (nonatomic,strong) UIView * mainView;
@property (nonatomic,strong) CBCColorView * colorView;
@property (nonatomic,strong) CBCFontSizeView * fontView;
@property (nonatomic,strong) CBCSliderView * sliderView;
@property (nonatomic,assign)NSInteger chapterIndex;
@end
@implementation CBCBottomMenuView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        
    }
    return self;
}
//设置ui视图
-(void)setupUI{
    
    [self setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]];
    [self addSubview:self.mainView];
    [self.mainView addSubview:self.catalogButton];
    [self.mainView addSubview:self.prgressButton];
    [self.mainView addSubview:self.fontButton];
    [self.mainView addSubview:self.backgroundButton];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [@[self.catalogButton, self.prgressButton, self.fontButton,self.backgroundButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:MenuPadding leadSpacing:MenuPadding tailSpacing:MenuPadding];
    
    [@[self.catalogButton, self.prgressButton, self.fontButton,self.backgroundButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.height.equalTo(self.fontButton.mas_width);
    }];
}
-(void)setReadModel:(CBCRecordModel *)readModel{
    _readModel = readModel;
    self.chapterIndex = readModel.chapter;
    if (self.chapterIndex+1==readModel.chapterCount) {
        self.sliderView.nextBtn.enabled = NO;
    }else{
        self.sliderView.nextBtn.enabled = YES;

    }
    if (self.chapterIndex==0) {
        self.sliderView.preBtn.enabled = NO;
    }else{
        self.sliderView.preBtn.enabled = YES;
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];

    [self updateFrame];

}
-(UIButton *)catalogButton
{
    if (!_catalogButton) {
        _catalogButton = [CBCReadUtilites commonButtonSEL:@selector(tapCatalog:) target:self];
        [_catalogButton setTitle:@"目录" forState:UIControlStateNormal];
       
        [_catalogButton setImage:[CommonFuctions cbcReaderImage:@"menuCatalog"] forState:UIControlStateNormal];
    }
    return _catalogButton;
}

-(UIButton *)prgressButton
{
    if (!_prgressButton) {
        _prgressButton = [CBCReadUtilites commonButtonSEL:@selector(tapProgress:) target:self];
        [_prgressButton setTitle:@"进度" forState:UIControlStateNormal];
          [_prgressButton setImage:[CommonFuctions cbcReaderImage:@"menuProgress"] forState:UIControlStateNormal];
//        [_prgressButton setImage:CBCReaderBundleImage(@"menuProgress") forState:UIControlStateNormal];
    }
    return _prgressButton;
}

-(UIButton *)fontButton
{
    if (!_fontButton) {
        _fontButton = [CBCReadUtilites commonButtonSEL:@selector(tapFont:) target:self];
        [_fontButton setTitle:@"字号" forState:UIControlStateNormal];
        [_fontButton setImage:[CommonFuctions cbcReaderImage:@"menuFont"] forState:UIControlStateNormal];
    }
    return _fontButton;
}

-(UIButton *)backgroundButton
{
    if (!_backgroundButton) {
        _backgroundButton = [CBCReadUtilites commonButtonSEL:@selector(tapBackground:) target:self];
        [_backgroundButton setTitle:@"背景" forState:UIControlStateNormal];
        [_backgroundButton setImage:[CommonFuctions cbcReaderImage:@"menuBackground"]  forState:UIControlStateNormal];
    }
    return _backgroundButton;
}

-(void)resetUI{
    [self removewSubViews];
    self.mainView.hidden = NO;
}

-(void)updateFrame{

    [_catalogButton verticalImageAndTitle:4];
    [_prgressButton verticalImageAndTitle:4];
    [_fontButton verticalImageAndTitle:4];
    [_backgroundButton verticalImageAndTitle:4];
}


//进度更新
- (void)didEndTouchesInRangeSlider:(TTRangeSlider *)sender{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(menuViewChapterProgress:)]) {
        
        CGFloat progress = (CGFloat)sender.selectedMaximum/100;

        [self.delegate menuViewChapterProgress:progress];
    }
}


//字体调整，是否增大？
-(void)fontChangeBtnClickIsLarge:(BOOL)large{
    if (large) {
        if (floor([CBCReadConfig shareInstance].fontSize) == floor(MaxFontSize)) {
            self.fontView.fontAddBtn.enabled = NO;
            return;
        }
        [CBCReadConfig shareInstance].fontSize++;
        self.fontView.fontReduceBtn.enabled = YES;
    }else{
        if (floor([CBCReadConfig shareInstance].fontSize) == floor(MinFontSize)){
            self.fontView.fontReduceBtn.enabled = NO;
            return;
        }
        [CBCReadConfig shareInstance].fontSize--;
        self.fontView.fontAddBtn.enabled = YES;

    }
    if (self.delegate&&[self.delegate respondsToSelector:@selector(menuViewFontSize:)]) {
        
        [self.delegate menuViewFontSize:nil];
    }
}
//点击章节
-(void)tapCatalog:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(menuViewInvokeCatalog:)]) {
        [self.delegate menuViewInvokeCatalog:self];
    }
}

//点击进度
-(void)tapProgress:(UIButton *)sender{
    [self removewSubViews];
    self.mainView.hidden = YES;
    [self addSubview:self.sliderView];
    self.sliderView.slider.selectedMaximum = self.currentProgress * 100;
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

//下一章
-(void)nextChapterAction:(UIButton*)btn{
    if (self.chapterIndex+1<=_readModel.chapterCount) {
        
        if ([self.delegate respondsToSelector:@selector(menuViewJumpChapter:page:)]) {
            self.chapterIndex++;
            self.sliderView.preBtn.enabled = YES;
            if (self.chapterIndex+1==_readModel.chapterCount) {
                btn.enabled = NO;
            }else{
                btn.enabled = YES;
            }
            [self.delegate menuViewJumpChapter:self.chapterIndex page:0];
            self.sliderView.slider.selectedMaximum = 0.0;

        }
    }
}

//上一章
-(void)preChapterAction:(UIButton*)btn{
    if (self.chapterIndex>0) {
        if ([self.delegate respondsToSelector:@selector(menuViewJumpChapter:page:)]) {
            self.chapterIndex--;
            self.sliderView.nextBtn.enabled = YES;
            if (self.chapterIndex==0) {
                btn.enabled = NO;
            }else{
                btn.enabled = YES;
            }
            self.sliderView.slider.selectedMaximum = 0.0;
            [self.delegate menuViewJumpChapter:self.chapterIndex page:0];
        }
    }
}
-(void)tapFont:(UIButton *)sender{
    [self removewSubViews];
    self.mainView.hidden = YES;
    [self addSubview:self.fontView];
    [self.fontView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

-(void)tapBackground:(UIButton *)sender{
    [self removewSubViews];
    self.mainView.hidden = YES;
    [self addSubview:self.colorView];
    [self.colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

-(void)removewSubViews{
    for (UIView * view in self.subviews)
    {
        if (view != self.mainView) {
            [view removeFromSuperview];
        }
    }
}
-(UIView *)mainView{
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        
    }return _mainView;
}
-(CBCColorView *)colorView{
    if (!_colorView) {
        _colorView = [[CBCColorView alloc]init];
    }return _colorView;
}
-(CBCFontSizeView *)fontView{
    if (!_fontView) {
        _fontView = [[CBCFontSizeView alloc]init];
        _fontView.delegate = self;
    }return _fontView;
}
-(CBCSliderView *)sliderView{
    if (!_sliderView) {
        _sliderView = [[CBCSliderView alloc]init];
        _sliderView.slider.delegate = self;
        [_sliderView.nextBtn addTarget:self action:@selector(nextChapterAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_sliderView.preBtn addTarget:self action:@selector(preChapterAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }return _sliderView;
}

@end
